package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.AuthService;
import cc.rengu.igas.channel.upac.facade.request.RealNameAuthRequest;
import cc.rengu.igas.channel.upac.facade.response.RealNameAuthResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 实名认证.
 */
public class RealNameAuthService extends AuthService {


    @Override
    protected Object beforeBizProcess() throws Exception {
        RealNameAuthRequest realNameAuthRequest = new RealNameAuthRequest();
        ConvertUtil.convertOutput(realNameAuthRequest);
        return realNameAuthRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        RealNameAuthResponse realNameAuthResponse = new RealNameAuthResponse();
        RealNameAuthRequest realNameAuthRequest = (RealNameAuthRequest) request;
        String payAcctNo = realNameAuthRequest.getPayAcctNo();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        if (AppParamConstant.YES.equals(acctEncFlag)) {
            realNameAuthRequest.setPayAcctNo(encryptData(payAcctNo));
        }
        ConvertUtil.convertInput(realNameAuthRequest);
        /*customer信息转换*/
        if (null != realNameAuthRequest.getCustomerInfo()) {
            packageCustomerInfo(realNameAuthRequest.getCustomerInfo(), realNameAuthRequest.getPayAcctNo());
        }
        if (null != realNameAuthRequest.getRiskCtrlInfo()) {
            packageRiskCtrlInfo(realNameAuthRequest.getRiskCtrlInfo());
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "85");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "03");
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, realNameAuthRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            realNameAuthResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            realNameAuthResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(realNameAuthResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(realNameAuthResponse);
        realNameAuthResponse.setRespCode(convertRespCode(realNameAuthResponse.getRespCode()));

        chnlBizResponse.setResult(realNameAuthResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        RealNameAuthResponse realNameAuthResponse = (RealNameAuthResponse) response.getResult();
        ConvertUtil.convertInput(realNameAuthResponse);
    }
}
